﻿namespace UnityEditor
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using UnityEditorInternal;
    using UnityEngine;

    [CustomEditor(typeof(EditorSettings))]
    internal class EditorSettingsInspector : Editor
    {
        private PopupElement[] serializationPopupList = new PopupElement[] { new PopupElement("Mixed", false, false), new PopupElement("Force Binary", false, false), new PopupElement("Force Text", false, true) };
        private PopupElement[] vcDefaultPopupList = new PopupElement[] { new PopupElement(ExternalVersionControl.Disabled, false, false), new PopupElement(ExternalVersionControl.Generic, false, false), new PopupElement(ExternalVersionControl.AssetServer, true, false) };
        private PopupElement[] vcPopupList;

        private void DoPopup(Rect popupRect, PopupElement[] elements, int selectedIndex, GenericMenu.MenuFunction2 func)
        {
            GenericMenu menu = new GenericMenu();
            for (int i = 0; i < elements.Length; i++)
            {
                PopupElement element = elements[i];
                if (element.Enabled)
                {
                    menu.AddItem(element.content, i == selectedIndex, func, i);
                }
                else
                {
                    menu.AddDisabledItem(element.content);
                }
            }
            menu.DropDown(popupRect);
        }

        public void OnEnable()
        {
            if (VCProvider.isPresent)
            {
                VCPlugin[] availablePlugins = VCProvider.availablePlugins;
                this.vcPopupList = new PopupElement[availablePlugins.Length + this.vcDefaultPopupList.Length];
                Array.Copy(this.vcDefaultPopupList, this.vcPopupList, this.vcDefaultPopupList.Length);
                int index = 0;
                int length = this.vcDefaultPopupList.Length;
                while (length < this.vcPopupList.Length)
                {
                    this.vcPopupList[length] = new PopupElement(availablePlugins[index].name, true, false);
                    length++;
                    index++;
                }
            }
            else
            {
                this.vcPopupList = this.vcDefaultPopupList;
            }
        }

        public override void OnInspectorGUI()
        {
            <OnInspectorGUI>c__AnonStorey3A storeya = new <OnInspectorGUI>c__AnonStorey3A();
            bool enabled = GUI.enabled;
            GUI.enabled = true;
            GUILayout.Label("Version Control", EditorStyles.boldLabel, new GUILayoutOption[0]);
            GUI.enabled = enabled;
            storeya.selvc = EditorSettings.externalVersionControl;
            int index = Array.FindIndex<PopupElement>(this.vcPopupList, new Predicate<PopupElement>(storeya.<>m__6D));
            if (index < 0)
            {
                index = 0;
            }
            GUIContent content = new GUIContent(this.vcPopupList[index].content);
            Rect position = EditorGUI.PrefixLabel(GUILayoutUtility.GetRect(content, EditorStyles.popup), 0, new GUIContent("Mode"));
            if (EditorGUI.ButtonMouseDown(position, content, FocusType.Passive, EditorStyles.popup))
            {
                this.DoPopup(position, this.vcPopupList, index, new GenericMenu.MenuFunction2(this.SetVersionControlSystem));
            }
            if (this.VersionControlSystemHasGUI() && VCProvider.isPresent)
            {
                GUI.enabled = true;
                if (EditorSettings.externalVersionControl == ExternalVersionControl.AssetServer)
                {
                    EditorUserSettings.SetConfigValue("vcUsername", EditorGUILayout.TextField("User", EditorUserSettings.GetConfigValue("vcUsername"), new GUILayoutOption[0]));
                    EditorUserSettings.SetConfigValue("vcPassword", EditorGUILayout.PasswordField("Password", EditorUserSettings.GetConfigValue("vcPassword"), new GUILayoutOption[0]));
                }
                else if ((EditorSettings.externalVersionControl != ExternalVersionControl.Generic) && (EditorSettings.externalVersionControl != ExternalVersionControl.Disabled))
                {
                    VCPlugin plugin = VCProvider.Lookup(EditorSettings.externalVersionControl);
                    if (!string.IsNullOrEmpty(plugin.name))
                    {
                        foreach (VCConfigField field in plugin.configFields)
                        {
                            string configValue = EditorUserSettings.GetConfigValue(field.name);
                            string str2 = configValue;
                            if (field.isPassword)
                            {
                                str2 = EditorGUILayout.PasswordField(field.label, configValue, new GUILayoutOption[0]);
                            }
                            else
                            {
                                str2 = EditorGUILayout.TextField(field.label, configValue, new GUILayoutOption[0]);
                            }
                            if (str2 != configValue)
                            {
                                EditorUserSettings.SetConfigValue(field.name, str2);
                            }
                        }
                    }
                    else
                    {
                        Debug.LogError("Cannot find version control plugin for " + EditorSettings.externalVersionControl.ToString());
                    }
                }
                GUILayout.BeginHorizontal(new GUILayoutOption[0]);
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("Connect", EditorStyles.miniButton, new GUILayoutOption[0]))
                {
                    VCProvider.UpdateSettings();
                }
                GUILayout.EndHorizontal();
                EditorUserSettings.AutomaticAdd = EditorGUILayout.Toggle("Automatic add", EditorUserSettings.AutomaticAdd, new GUILayoutOption[0]);
                EditorUserSettings.WorkOffline = EditorGUILayout.Toggle("Work Offline", EditorUserSettings.WorkOffline, new GUILayoutOption[0]);
                if (Unsupported.IsDeveloperBuild())
                {
                    EditorUserSettings.DebugCmd = EditorGUILayout.Toggle("Debug Cmds", EditorUserSettings.DebugCmd, new GUILayoutOption[0]);
                    EditorUserSettings.DebugOut = EditorGUILayout.Toggle("Debug Output", EditorUserSettings.DebugOut, new GUILayoutOption[0]);
                    EditorUserSettings.DebugCom = EditorGUILayout.Toggle("Debug Coms", EditorUserSettings.DebugCom, new GUILayoutOption[0]);
                }
                GUI.enabled = enabled;
            }
            GUILayout.Space(10f);
            GUILayout.Label("WWW Security Emulation", EditorStyles.boldLabel, new GUILayoutOption[0]);
            string str3 = EditorGUILayout.TextField("Host URL", EditorSettings.webSecurityEmulationHostUrl, new GUILayoutOption[0]);
            if (str3 != EditorSettings.webSecurityEmulationHostUrl)
            {
                EditorSettings.webSecurityEmulationHostUrl = str3;
            }
            GUILayout.Space(10f);
            GUI.enabled = true;
            GUILayout.Label("Asset Serialization", EditorStyles.boldLabel, new GUILayoutOption[0]);
            content = new GUIContent(this.serializationPopupList[(int) EditorSettings.serializationMode].content);
            position = EditorGUI.PrefixLabel(GUILayoutUtility.GetRect(content, EditorStyles.popup), 0, new GUIContent("Mode"));
            if (EditorGUI.ButtonMouseDown(position, content, FocusType.Passive, EditorStyles.popup))
            {
                this.DoPopup(position, this.serializationPopupList, (int) EditorSettings.serializationMode, new GenericMenu.MenuFunction2(this.SetAssetSerializationMode));
            }
            GUI.enabled = enabled;
        }

        private void SetAssetSerializationMode(object data)
        {
            int num = (int) data;
            EditorSettings.serializationMode = (SerializationMode) num;
        }

        private void SetVersionControlSystem(object data)
        {
            int index = (int) data;
            if ((index >= 0) || (index < this.vcPopupList.Length))
            {
                PopupElement element = this.vcPopupList[index];
                EditorSettings.externalVersionControl = element.content.text;
                AssetDatabase.Refresh();
            }
        }

        private bool VersionControlSystemHasGUI()
        {
            ExternalVersionControl externalVersionControl = EditorSettings.externalVersionControl;
            return ((VCProvider.Lookup((string) externalVersionControl) != null) && ((((externalVersionControl != ExternalVersionControl.Disabled) && (externalVersionControl != ExternalVersionControl.AutoDetect)) && (externalVersionControl != ExternalVersionControl.AssetServer)) && (externalVersionControl != ExternalVersionControl.Generic)));
        }

        [CompilerGenerated]
        private sealed class <OnInspectorGUI>c__AnonStorey3A
        {
            internal ExternalVersionControl selvc;

            internal bool <>m__6D(EditorSettingsInspector.PopupElement cand)
            {
                return (cand.content.text == this.selvc);
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct PopupElement
        {
            public readonly bool requiresTeamLicense;
            public readonly bool requiresProLicense;
            public readonly GUIContent content;
            public PopupElement(string content, bool requiresTeamLicense, bool requiresProLicense)
            {
                this.content = new GUIContent(content);
                this.requiresTeamLicense = requiresTeamLicense;
                this.requiresProLicense = requiresProLicense;
            }

            public bool Enabled
            {
                get
                {
                    return ((!this.requiresTeamLicense || InternalEditorUtility.HasMaint()) && (!this.requiresProLicense || InternalEditorUtility.HasPro()));
                }
            }
        }
    }
}

